package 笔试;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RedPack2Test {

    private static List<Integer> divide(double money, int n) {
        int fen = (int) (money * 100);
        if (fen < n || n < 1) {
            System.out.println("红包个数必须大于0，并且最小红包不少于1分");
            return new ArrayList();
        }
        List<Integer> boards = new ArrayList<>();
        boards.add(0);
        boards.add(fen);

        //红包个数和板砖个数的关系
        while (boards.size() <= n) {
            int index = new Random().nextInt(fen - 1) + 1;
            if (boards.contains(index)) {
                //保证板子的位置不相同
                continue;
            }
            boards.add(index);
        }
        //计算每个红包的金额，将两个板子之间的钱加起来
        Collections.sort(boards);
        List list = new ArrayList<>();
        for (int i = 0; i < boards.size() - 1; i++) {
            Integer e = boards.get(i + 1) - boards.get(i);
            list.add(e);
        }
        return list;
    }

    public static void main(String[] args) {

        List<Integer> accountList = divide(1, 3);

        BigDecimal count = new BigDecimal(0);

        for (Integer amount : accountList) {
            //将抢到的金额再除以100进行还原
            BigDecimal tmpcount = new BigDecimal(amount).divide(new BigDecimal(100));
            count = count.add(tmpcount);
            System.out.println("抢到金额：" + tmpcount);
        }

        System.out.println("total=" + count);

    }
}